<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<%@ include file="../root.jsp" %>
</head>
<body  class="easyui-layout" fit="true">
<div  region="center">

<div id="import_excel">
<form id="import_excel_form" method="post"  enctype="multipart/form-data" style="padding:10px 20px 10px 40px;">
	<div style="margin-bottom:20px">
            <input type="hidden" name="file_name" id="file_name">
			<div>选择文件:</div>
			<input class="easyui-filebox" name="excel_file" id="excel_file" data-options="required:'true',prompt:'选择需要上传的文件...',buttonText:'选择文件',accept:'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'" style="width:100%">
		</div>
		<div>
			<a id="uploadExcel" class="easyui-linkbutton" style="width:100%">上传</a>
		</div>
</form>
</div>
    
 <div id="add_deviceType">
<form id="add_deviceType_form" method="post" style="padding:10px 20px 10px 40px;">
    <table cellpadding="5">
        <tr>
            <td>模板名称：</td>
            <td><input type="text" name="deviceName" class="easyui-textbox"  data-options="required:'true'"></td>
        </tr>
        <tr>
            <td>
                类名称：
            </td>
            <td>
                <input type="text" name="targetName" class="easyui-textbox"  data-options="required:'true'">
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                            <a href="javascript:void(0)" class="easyui-linkbutton" id="add_deviceType_form_submit">提交</a>
                            &nbsp;&nbsp;
                            <a href="javascript:void(0)" class="easyui-linkbutton" id="add_deviceType_form_clear">清除</a>
            </td>
        </tr>
    </table>
</form>
</div>
<div id="dp-datagrid"></div>
</div>
<script type="text/javascript">
$(function(){
	$('#dp-datagrid').datagrid({
		title:'设备类表',
		//表单默认排序字段
		sortName:'deviceName',
		sortOrder:'asc',
		view: detailview,
        pagination:true,
        border:false,
        pageSize:50,  
        pageNumber:1,
        singleSelect:false,
        url: '${basePath}dp/getDeviceType',  
        loadMsg:'加载中...',  
        fitColumns: true, 
        fit: true, 
        rowStyler:function(index,row){    
            return 'background-color:#B0E0E6;color:blue;font-weight:bold;';    
    	},  
        columns: [[  
                   { field: 'id', checkbox: true},  
                   //{ field: 'tid', title: '编号', width:60 },  
                   { field: 'deviceName', title: '名称', width:150}
               ]],toolbar: [{  
                   text:'上传EXCEL',  
                   iconCls:'icon-add',  
                   handler:function(){  
                	   importExcel();  
                   }  
               },'-',{  
                   text:'下载模板',  
                   iconCls:'icon-edit',  
                   handler:function(){  
                       downLoadModel();  
                   }  
               },'-',{  
                   text:'删除',  
                   iconCls:'icon-remove',  
                   handler:function(){  
                       deleteRow();  
                   }  
               },'-',{  
                   text:'导出EXCEL',  
                   iconCls:'icon-sum',  
                   handler:function(){  
                       exportExcel();  
                   }  
               },'-',{  
                   text:'根据模板创建设备类',  
                   iconCls:'icon-add',  
                   handler:function(){  
                       addDeviceTypeByModel();  
                   }  
               } ],
       	detailFormatter:function(index,row){
			return '<div style="padding:2px"><table id="ddv-' + index + '"></table></div>';
		},
        onExpandRow: function(index,row){  
            $('#ddv-'+index).datagrid({  
                fitColumns:true,  
                singleSelect:true,  
                rownumbers:true,  
                loadMsg:'',  
                height:'auto',
                border:false,
                data:row.attrList,
                columns: [[
                           //{ field: 'id', checkbox: true},  
	                       //{ field: 'aid', title: '属性编号', width:60 },  
	                       { field: 'type', title: 'type', width:80,formatter:function(value,row,index){
		                    	 	return row.type;
	                    	   }
	                       },  
	                       { field: 'index', title: 'index', width:100,formatter:function(value,row,index){
		                    	 	return row.index;
	                    	   } },
	                       { field: 'ioType', title: 'ioType', width:100,formatter:function(value,row,index){
		                    	 	return row.ioType;
	                    	   } }, 
	                       { field: 'dataType', title: 'dataType', width:150,formatter:function(value,row,index){
		                    	 	return row.dataType;
	                    	   } }
                       ]],  
                onResize:function(){  
                    $('#dp-datagrid').datagrid('fixDetailRowHeight',index);  
                },  
                onLoadSuccess:function(){
                    setTimeout(function(){  
                        $('#dp-datagrid').datagrid('fixDetailRowHeight',index);
                    },0);  
                },
                onClickRow: function (rowIndex, rowData) {
                    $(this).datagrid('unselectRow', rowIndex);
                }
            });  
            $('#dp-datagrid').datagrid('fixDetailRowHeight',index);  
        },onLoadSuccess:function(){
        	$('#dp-datagrid').datagrid('clearSelections');
        	var me = this;  
       	  	setTimeout(function () {//延时触发easyui datagrid detailviewclick事件，不用计时器无法展开,EASYUI本身限制
       	    //$(me).parent().find('span.datagrid-row-expander').trigger('click'); //没效果注意修改这里的选择器
       	   	}, 10);
        	
        },onClickRow: function (rowIndex, rowData) {
            $(this).datagrid('unselectRow', rowIndex);
        }
        });
	
	function importExcel()
	{
		$('#import_excel_form').form('clear');
		$("#import_excel").dialog('open');
	}
        
        function downLoadModel()
        {
            window.location.href='${basePath}file/device_type_model.xlsx';
        }
	
	function uploadExcel()
	{
		$('#import_excel_form').form('submit',{
			url: '${basePath}excel/uploadDpType', 
	        onSubmit: function () {        //表单提交前的回调函数
                  console.log("do import");
                  var fileName = $('#excel_file').filebox('getText');
                  $('#file_name').attr('value',fileName)
	          return  $(this).form('validate'); // 如果验证不通过，返回false终止表单提交 
	        }, 
	        success: function (data){
	        	var jsonObj = eval("("+data+")");
	        	if ("success" == jsonObj.type) {
	            $.messager.show({ 
	              title: '提示消息', 
	              msg: jsonObj.message, 
	              showType: 'show', 
	              timeout: 1000, 
	              style: { 
	                right: '', 
	                bottom: '' 
	              } 
	            }); 
	            $('#dp-datagrid').datagrid('reload');  // 重新载入当前页面数据  
	          }else if("warning"== jsonObj.type){
	        	  $.messager.alert('提示信息', jsonObj.message, 'warning'); 
	          } 
	          else { 
	            $.messager.alert('提示信息', jsonObj.message, 'error'); 
	        }
	        }
		})
	}
        
        function addDeviceTypeByModel()
        {
            $('#add_deviceType_form').form("clear");
            $('#add_deviceType').dialog("open");
        }
        
        $('#add_deviceType_form_clear').on('click',function(){
            $('#add_deviceType_form').form("clear");
        })
        
        $('#add_deviceType_form_submit').on('click',function(){
            addDeviceType();
        });
        
        function addDeviceType()
        {
            $('#add_deviceType_form').form('submit',{
			url: '${basePath}dp/addDeviceTypeByModel', 
	        onSubmit: function () {        //表单提交前的回调函数
                console.log('do add');
	          return  $('#add_deviceType_form').form('validate'); // 如果验证不通过，返回false终止表单提交 
	        }, 
	        success: function (data){
	        	var jsonObj = eval("("+data+")");
	        	if ("success" == jsonObj.code) {
	            $.messager.show({ 
	              title: '提示消息', 
	              msg: jsonObj.message, 
	              showType: 'show', 
	              timeout: 1000, 
	              style: { 
	                right: '', 
	                bottom: '' 
	              } 
	            }); 
	            $('#dp-datagrid').datagrid('reload');  // 重新载入当前页面数据  
	            $('#add_deviceType').dialog('close'); //关闭窗口 
	          }else if("warning"== jsonObj.code){
	        	  $.messager.alert('提示信息', jsonObj.message, 'warning'); 
	          } 
	          else { 
	            $.messager.alert('提示信息', jsonObj.message, 'error'); 
	        }
	        }
		})
        }

		function updaterRow()
                {
                    var rows = $('#dp-datagrid').datagrid('getSelections');
                    if(rows.length == 0)
                    {  
                        $.messager.alert('提示',"请选择需要修改的记录",'warning');  
                        return;  
                    }  
                    if(rows.length > 1)
                    {  
                        $.messager.alert('提示',"只能选择一项记录进行修改",'warning');  
                        return;  
                    } 
                    $("#add_goods").form('load', rows[0]);
                    targetUrl = "${basePath}goods/updateGoods";
                    $("#add_goods").dialog('open');
		}
                
       function deleteRow(){
			var rows = $('#dp-datagrid').datagrid('getSelections');
			var tids = "";
			if(rows.length == 0){
				$.messager.alert('提示',"请选择需要修改的记录",'warning');  
                return; 
			}
			$.messager.confirm("删除提示", "确定删除"+rows.length+"条数据吗？", function (data) {
				if (data) {
					$(rows).each(function(){
	                	if(tids == ""){
	                		tids = this.tid;
	                    }else{
	                    	tids += ","+this.tid;
	                    }
	        		})
	        		var json={"tids":tids};
					sendAjaxRequest('${basePath}dp/deleteDeviceType', json,null, function(data){
			            var jsonObj = data.data;
			            	$.messager.show({ 
			  	              title: '提示消息', 
			  	              msg: jsonObj.message, 
			  	              showType: 'show', 
			  	              timeout: 1000, 
			  	              style: { 
			  	                right: '', 
			  	                bottom: '' 
			  	              } 
			  	        });	
			    	})
					$('#dp-datagrid').datagrid('reload');  // 重新载入当前页面数据  
	            }else {
	                return;
	            }
	        });
        }
        
        function exportExcel()
        {
            var rows = $('#dp-datagrid').datagrid('getSelections');
            var ids="";
            if(rows.length == 0)
            {  
                ids="";  
            }  
            if(rows.length > 1)
            {  
                $(rows).each(function()
                {
                    if(ids == "")
                    {
                            ids = this.tid;
                    }else{
                            ids += ","+this.tid;
                    }
		})
                ids ="("+ids+")";;  
            } 
            var url='${basePath}excel/downloadDpType';
            window.location.href=url+'?ids='+ids;
            
        }

        $("#add_deviceType").dialog({
	    title: '根据模板创建设备类',
	    width: 400,
	    height:200,
	    closed: true,
	    cache: false,
	    modal: true
	});
        
	$("#import_excel").dialog({
	    title: '导入EXCEL',
	    width: 400,
	    height:200,
	    closed: true,
	    cache: false,
	    modal: true
	});
	
	$("#uploadExcel").on('click',function(){
		uploadExcel();
	});
});
</script>
</body>
</html>